package com.hn.springcloud.controller;

import com.hn.springcloud.pojo.User;
import com.hn.springcloud.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * hanni
 * 2019/2/12 0012
 * 20:08
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RestTemplate template;

    @GetMapping
    public User getUser(){

        User user = template.getForObject("http://provider/user", User.class);

        return user;
    }

    @GetMapping("/add")
    public ResponseVo add(){

        User user = new User(200,"哈哈");

        //postForEntity返回的数据信息比较全
       /* ResponseEntity<ResponseVo> responseEntity = template.postForEntity("http://provider/user", user, ResponseVo.class);
        ResponseVo body = responseEntity.getBody();*/

        ResponseVo responseVo = template.postForObject("http://provider/user/a", user, ResponseVo.class);

        return  responseVo;
    }

    @GetMapping("/add2")
    public ResponseVo add2(){

//        ResponseEntity<ResponseVO> responseEntity = restTemplate.postForEntity("http://provider/user", u, ResponseVO.class);
//        ResponseVO body = responseEntity.getBody(); //返回结果里面的数据

        ResponseVo responseVo = template.postForObject("http://provider/user/a?id=1&name='悟空'", null, ResponseVo.class);
//        ResponseVO responseVO = restTemplate.postForObject("http://provider/user", new User(1,"AAA"), ResponseVO.class);

        //两种的 传参方式
        template.postForObject("http://provider/user/a?id={1}&name={2}",null,ResponseVo.class,1,"八节");

        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("myname","王五");

        template.postForObject("http://provider/user/a?id={id}&name={myname}",null,ResponseVo.class,map);

        return  responseVo;
    }

}
